package com.siruier.boss.bean;

import android.text.TextUtils;

import com.siruier.boss.BuildConfig;
import com.siruier.boss.ui.utils.SPUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by zhoucl on 2019/11/12 0012.
 */
public class ServiceConfig {
    public static String API;
    public static String H5;
    public static String TOTAL_SERVICE_TAG;
    public static String API_SERVICE_TAG;
    public static String H5_SERVICE_TAG;
    private static final List<ServiceConfig> SERVICE_CONFIGS = new ArrayList<>();
    private static CurrentServiceConfig CURRENT_SERVICE_CONFIG;
    public static final ServiceConfig
            RELEASE = new ServiceConfig(
            1,
            "正式",
            "release",
            "pro",
            BuildConfig.RELEASE_BASE_URL,
            BuildConfig.RELEASE_BASE_URL),
            DEBUG = new ServiceConfig(
                    2,
                    "测试",
                    "debug",
                    "dev",
                    BuildConfig.TEST_BASE_URL,
                    BuildConfig.TEST_BASE_URL);

    static {
        Field[] declaredFields = ServiceConfig.class.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            if (declaredField.getType() == ServiceConfig.class) {
                try {
                    SERVICE_CONFIGS.add((ServiceConfig) declaredField.get(null));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        Collections.sort(SERVICE_CONFIGS, (o1, o2) -> o1.order - o2.order);
    }

    public static void reset() {
        CURRENT_SERVICE_CONFIG = null;
    }

    public static CurrentServiceConfig getCurrentServiceConfig() {
        if (CURRENT_SERVICE_CONFIG == null) {
            SPUtils spUtils = SPUtils.INSTANCE;
            boolean saveLoca = spUtils.getBoolean(SpConstants.SP_KEY_SAVE_LOCA, false);
            ServiceConfig defaultServiceConfig = getDefaultServiceConfig();
            CurrentServiceConfig currentServiceConfig;
            if (saveLoca) {
                currentServiceConfig = new CurrentServiceConfig();
                currentServiceConfig.pushTag = defaultServiceConfig.pushTag;
                currentServiceConfig.apiUrl = spUtils.getString(SpConstants.SP_KEY_API, defaultServiceConfig.apiUrl);
                currentServiceConfig.h5Url = spUtils.getString(SpConstants.SP_KEY_H5, defaultServiceConfig.h5Url);
                currentServiceConfig.totalServiceTag = spUtils.getString(SpConstants.SP_KEY_TOTAL_SERVICE_TAG, "");
                currentServiceConfig.apiServiceTag = spUtils.getString(SpConstants.SP_KEY_API_SERVICE_TAG, "");
                currentServiceConfig.h5ServiceTag = spUtils.getString(SpConstants.SP_KEY_h5_SERVICE_TAG, "");
                return currentServiceConfig;
            } else {
                currentServiceConfig = new CurrentServiceConfig();
                currentServiceConfig.pushTag = defaultServiceConfig.pushTag;
                currentServiceConfig.apiUrl = API == null ? defaultServiceConfig.apiUrl : API;
                currentServiceConfig.h5Url = H5 == null ? defaultServiceConfig.h5Url : H5;
                currentServiceConfig.totalServiceTag = TOTAL_SERVICE_TAG == null ? defaultServiceConfig.serviceTag : TOTAL_SERVICE_TAG;
                currentServiceConfig.apiServiceTag = API_SERVICE_TAG == null ? defaultServiceConfig.serviceTag : API_SERVICE_TAG;
                currentServiceConfig.h5ServiceTag = H5_SERVICE_TAG == null ? defaultServiceConfig.serviceTag : H5_SERVICE_TAG;
            }
            CURRENT_SERVICE_CONFIG = currentServiceConfig;
        }
        return CURRENT_SERVICE_CONFIG;
    }

    private static ServiceConfig getDefaultServiceConfig() {
        ServiceConfig defaultServiceConfig = null;
        for (ServiceConfig serviceConfig : SERVICE_CONFIGS) {
            if (serviceConfig.serviceTag.equals(BuildConfig.BUILD_TYPE))
                defaultServiceConfig = serviceConfig;
        }
        if (defaultServiceConfig == null) {
            defaultServiceConfig = DEBUG;
        }
        return defaultServiceConfig;
    }

    public static List<ServiceConfig> getServiceConfigs() {
        List<ServiceConfig> serviceConfigs = new ArrayList<>();
        for (ServiceConfig serviceConfig : SERVICE_CONFIGS) {
            serviceConfigs.add(serviceConfig.copy());
        }
        return serviceConfigs;
    }

    public String serviceName;
    public String serviceTag;
    public String pushTag;
    public String h5Url;
    public String apiUrl;
    public boolean check;
    private int order;

    public ServiceConfig() {
    }

    public ServiceConfig(int order, String serviceName, String serviceTag, String pushTag, String h5Url, String apiUrl) {
        this.order = order;
        this.serviceName = serviceName;
        this.serviceTag = serviceTag;
        this.pushTag = pushTag;
        this.h5Url = h5Url;
        this.apiUrl = apiUrl;
    }

    public boolean isEmpty() {
        return TextUtils.isEmpty(h5Url) || TextUtils.isEmpty(apiUrl);
    }

    public ServiceConfig copy() {
        return new ServiceConfig(order, serviceName, serviceTag, pushTag, h5Url, apiUrl);
    }
}
